package us.mooresr.aircraftscheduling.client.ui;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.annotations.NameToken;

import us.mooresr.aircraftscheduling.client.DispatchCallback;
import us.mooresr.aircraftscheduling.client.events.UserLoggedInEvent;
import us.mooresr.aircraftscheduling.client.place.NameTokens;
import us.mooresr.aircraftscheduling.shared.action.LoginAction;
import us.mooresr.aircraftscheduling.shared.action.LoginResult;



import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

public class LoginPresenter extends
		Presenter<LoginPresenter.MyView, LoginPresenter.MyProxy> 
		implements LoginUiHandlers {

	public interface MyView extends View, HasUiHandlers<LoginUiHandlers>{
		void setBusyCursor();
		void setNormalCursor();
		void clear();
	}

	@ProxyStandard
	@NameToken(NameTokens.login)
	public interface MyProxy extends ProxyPlace<LoginPresenter> {
	}

	private final PlaceManager placeManager;
	private final DispatchAsync dispatcher;
	private final EventBus eventBus;
	
	
	@Inject
	public LoginPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, PlaceManager placeManager, DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		this.placeManager = placeManager;
		this.dispatcher = dispatcher;
		this.eventBus = eventBus;
		
		getView().setUiHandlers(this);
	}

	@Override
	protected void revealInParent() {
		// RevealContentEvent.fire(eventBus, MainPagePresenter.TYPE_SetMainContent, this);
		RevealRootContentEvent.fire(this, this);
	}

	@Override
	public void doLogin(String user, String password) {
		getView().setBusyCursor();
		dispatcher.execute(new LoginAction(user, password), new DispatchCallback<LoginResult>() {

			@Override
			public void onSuccess(LoginResult result) {
				if (result.getUserDetail() == null) {
					Window.alert("Login failed");
					getView().setNormalCursor();
				} else {
					UserLoggedInEvent.fire(getEventBus(), result.getUserDetail());
					getView().setNormalCursor();
					getView().clear();
					
					
					PlaceRequest placeRequest = new PlaceRequest(NameTokens.reservationlist);
					placeManager.revealPlace(placeRequest); 
				}
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Login failed due to internal error");
				getView().setNormalCursor();
			}
			
		});
	}
}
